package com.view.components;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;


public class ImageUtil {
// raw is the image array.
    public static void blend(int[] raw, int alphaValue, int maskColor, int dontmaskColor){
        int len = raw.length;
        
        // Start loop through all the pixels in the image.
        for(int i=0; i<len; i++){
            int a = 0;
            int color = (raw[i] & 0x00FFFFFF); // get the color of the pixel.
            if(maskColor==color){
                a = 0;
            }else if(dontmaskColor==color){
                a = 255;
            }else if(alphaValue>0){
                a = alphaValue;     // set the alpha value we vant to use 0-255.
            }
            
            a = (a<<24);    // left shift the alpha value 24 bits.
            color += a;
            raw[i] = color;
        }
    }
    public static void blend(int[] raw, int alphaValue){
        blend(raw, alphaValue, 0xFFFFFFFF, 0xFFFFFFFF);
    }
    
    public static Image createThumbnail(Image image, int thumbWidth, int thumbHeight) {
        if(thumbWidth < 1 && thumbHeight < 1)
            return null;
        
        int sourceWidth = image.getWidth();
        int sourceHeight = image.getHeight();
        
        if (thumbHeight < 1){
            thumbHeight = thumbWidth * sourceHeight / sourceWidth;
        } else if(thumbWidth < 1){
            thumbWidth = thumbHeight *  sourceWidth / sourceHeight;
        }
        
        Image thumb = Image.createImage(thumbWidth, thumbHeight);
        Graphics g = thumb.getGraphics();
        
        
        
        ///*
        for (int y = 0; y < thumbHeight; y++) {
            for (int x = 0; x < thumbWidth; x++) {
                g.setClip(x, y, 1, 1);
                int dx = x * sourceWidth / thumbWidth;
                int dy = y * sourceHeight / thumbHeight;
                g.drawImage(image, x - dx, y - dy, Graphics.LEFT| Graphics.TOP);
                
            }
        }
        Image immutableThumb = Image.createImage(thumb);
        
        return immutableThumb;
    }
    
    public static byte[] encodeImage(Image img) throws IOException{
        ByteArrayOutputStream bOut = new ByteArrayOutputStream();
        DataOutputStream dOut = new DataOutputStream(bOut);
        
        dOut.writeInt(img.getWidth());
        dOut.writeInt(img.getHeight());
        int[] pixels = new int[img.getWidth()*img.getHeight()];
        img.getRGB(pixels, 0, img.getWidth(), 0, 0, img.getWidth(), img.getHeight());
        for(int i=0; i<pixels.length; i++){
            dOut.writeInt(pixels[i]);
        }
        return bOut.toByteArray();
    }
    
    public static Image decodeImage(byte[] data) throws IOException{
        ByteArrayInputStream bIn = new ByteArrayInputStream(data);
        DataInputStream dIn = new DataInputStream(bIn);
        
        int height = dIn.readInt();
        int width = dIn.readInt();
        int tam = height*width;
        Image img = Image.createImage(width, height);
        Graphics g = img.getGraphics();
        
        int[] pixels = new int[tam];
        for(int i=0; i<tam; i++){
            pixels[i] = dIn.readInt();
        }
        g.drawRGB(pixels, 0, width, 0, 0, width, height, false);
        
        return Image.createImage(img);
    }
    
    
    
    
}
